package com.all.utils;

import android.annotation.SuppressLint;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class DateUtils {
	
	 private static Calendar calendar = Calendar.getInstance();
	/**
	 * 获取当前日期format(yyyy-MM-dd)
	 * 
	 * @return
	 */
	public static String getNowDate() {
//		Calendar calendar = Calendar.getInstance();
//		int mTodayYear = calendar.get(Calendar.YEAR);
//		int mTodayMonth = calendar.get(Calendar.MONTH) + 1;
//		int mTodayDay = calendar.get(Calendar.DAY_OF_MONTH);
//		return mTodayYear + "-" + mTodayMonth + "-" + mTodayDay;
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Date currentTime = new Date();
		String timeNow = format1.format(currentTime);
		return timeNow;
	}

	/**
	 * 获取当前日期format(HH-mm)
	 * 
	 * @return
	 */
	public static String getNowTime() {
		
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		return hour + ":" + minute;
	}
	
	/**
	 * 获取当前日期 时间
	 * @return
	 */
	public static String getDateTimeNow() {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date currentTime = new Date();
		String timeNow = format1.format(currentTime);
		return timeNow;
	}
	
	/**
	 * 获取Date格式时间
	 * @param strDate
	 * @return
	 */
	public static Date getDateFromString(String strDate) {
		DateFormat format = new SimpleDateFormat("yyyy/MM/dd E");        
		Date date = null;   
		try {   
		    date = format.parse(strDate);  // Thu Jan 18 00:00:00 CST 2007   
		} catch (ParseException e) {   
		    e.printStackTrace();  
		    return null;
		}   
		return date;
	}
	
	/**
	 * 获取Date格式时间
	 * @param strDate [2014,9,1]
	 * qinyongqian
	 * 2014年9月13日
	 *
	 */
	public static Date getDateFromString2(String strDate) {
		strDate= strDate.replace("[", "").replace("]", "").replace(",","-");
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");        
		Date date = null;   
		try {   
		    date = format.parse(strDate);  //  
		} catch (ParseException e) {   
		    e.printStackTrace();  
		    return null;
		}   
		return date;
	}
	
	
	
	/**
	 * Date转成String
	 * 
	 * qinyongqian
	 * 2014年9月13日
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getStrDateFromDate(Date date){
		String str="";
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		    str=sdf.format(date); 
		} catch (Exception e) {
			// TODO: handle exception
			return "";
		}
		return str;
	}
	
	/**
	 * Date转成String
	 * 
	 * qinyongqian
	 * 2014年9月13日
	 * @return yyyy-MM-dd
	 */
	public static String getStrDateFromDate2(Date date){
		String str="";
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
		    str=sdf.format(date); 
		} catch (Exception e) {
			// TODO: handle exception
			return "";
		}
		return str;
	}
	
	/**
	 * 
	 * @ClassName:DateUtils.java
	 * @Description: 获取String格式时间 2014-9-1
	 * @param strDate [2014,9,1]
	 * @return
	 * @author qinyongqian
	 * @date 2014年11月26日
	 */
	public static String getStrDateFromString(String strDate) {
		strDate= strDate.replace("[", "").replace("]", "").replace(",","-");
		return strDate;
	}
	
	/**
	 * 获取当前日期,指定时间
	 * @return
	 */
	public static String getDateTime(int hh,int mm,int ss) {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Date currentTime = new Date();
		String time = format1.format(currentTime)+" "+hh+":"+mm+":"+ss;
		return time;
	}
	
	/**
	 * 获取字符串格式时间 1900-01-01
	 * @param strDate 19000101
	 * @return
	 */
	public static String getDateStrFromString(String strDate) {
		String y = strDate.substring(0, 4);
		String m = strDate.substring(4, 6);
		String d = strDate.substring(6);
		return y + "-" + m + "-" + d;
	}
	//	判断当前月份的最大天数
	public static int getDaysOfMonth()
	{
		int days=calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 
		return days;
	}

	public static String getFistDayOfMonth()
	{
		//------------------------取当月的第一天------------------------

		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-01"); 
		 Date firstDay=new Date(); 
		 return format.format(firstDay);
	}

	public static String getLastDayOfMonth()
	{
		//------------------------取当月的最后一天------------------------

		 int maxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 
		 java.text.Format formatter3=new SimpleDateFormat("yyyy-MM-"+maxDay);
		 return formatter3.format(calendar.getTime());
	}

	 private static SimpleDateFormat sf = null;
	    /*获取系统时间 格式为："yyyy/MM/dd "*/
	    public static String getCurrentDate() {
	        Date d = new Date();
	         sf = new SimpleDateFormat("yyyy年MM月dd日");
	        return sf.format(d);
	    }
	                                     
	    /*时间戳转换成字符窜*/
	    public static String getDateToString(long time) {
	        Date d = new Date(time);
	        sf = new SimpleDateFormat("yyyy年MM月dd日");
	        return sf.format(d);
	    }
	                                     
	    /*将字符串转为时间戳*/
	    public static long getStringToDate(String time) {
	        sf = new SimpleDateFormat("yyyy年MM月dd日");
	        Date date = new Date();
	        try{
	            date = sf.parse(time);
	        } catch(ParseException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        return date.getTime();
	    }
	
}
